﻿Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Text.RegularExpressions

Public Class frmRepairTool

    Private bSecondPass As Boolean = False
    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        OpenFileDialog1.Title = "Select one file to repait it"
        OpenFileDialog1.Multiselect = False
        OpenFileDialog1.Filter = "*.aspx|*.aspx"
        Dim res As DialogResult = OpenFileDialog1.ShowDialog()
        If Not (res = Windows.Forms.DialogResult.Abort Or res = Windows.Forms.DialogResult.Cancel) Then
            txtURL.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub btnRepair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRepair.Click
        bSecondPass = False
        lstLog.Items.Clear()
        Dim strFile As String, strRes As String, sERR As String = ""
        lstLog.Items.Add("Opening File ....")
        lstLog.Refresh()
        strFile = GetFileContentsLineByLine(txtURL.Text, sERR)
        If sERR = "" Then
            lstLog.Items.Add("Repairing ....")
            'Para Ficheros muy grandes, se cuelgan las expresiones regulares...
            'Lo hacemos linea por linea mejor.
            'strRes = Regex.Replace(strFile, "[ \t]+[\s]*\n", "", RegexOptions.Multiline)
            strRes = strFile
            lstLog.Items.Add("making backup ...." & txtURL.Text & ".bak")
            File.Copy(txtURL.Text, txtURL.Text & ".bak", True)
            'fl.Open(txtURL.Text, FileMode.Create, FileAccess.Write)
            lstLog.Items.Add("saving file....")
            SaveTextToFile(strRes, txtURL.Text, sERR)
            If bSecondPass = True Then
                lstLog.Items.Add("Doing Second pass ....")
                bSecondPass = False
                strFile = GetFileContentsLineByLine(txtURL.Text, sERR)
                strRes = strFile
                If sERR = "" Then
                    lstLog.Items.Add("saving file again....")
                    SaveTextToFile(strRes, txtURL.Text, sERR)
                Else
                    lstLog.Items.Add("An error has ocurred openning file.")
                End If
            End If
            lstLog.Items.Add("Success! The file is Repaired.")
            lstLog.Items.Add("Remember to upload it, if it is local file system.")
        Else
            lstLog.Items.Add("Error." & sERR)
        End If
    End Sub

    Public Function GetFileContents(ByVal FullPath As String, _
       Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
    End Function

    Public Function GetFileContentsLineByLine(ByVal FullPath As String, _
   Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String = "", linea As String = ""
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            While Not objReader.EndOfStream
                linea = objReader.ReadLine
                If InStr(linea, "&amp;#xD;&amp;#xA;") > 0 And bSecondPass = False Then
                    bSecondPass = True
                    lstLog.Items.Add("Characters &amp;#xD;&amp;#xA; found a second pass is needed ....")
                End If

                linea = Replace(linea, "&amp;#xD;&amp;#xA;", vbCrLf)
                If Trim(Replace(Replace(Replace(linea, vbTab, ""), vbCr, ""), vbLf, "")) <> "" Then
                    strContents = strContents + linea & vbCrLf

                    'Debug.Print(linea)

                End If
            End While

            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
    End Function

    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim Contents As String
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try


            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Process.Start("http://www.robertomarcos.com")
    End Sub

    Private Sub LinkLabel3_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
        Process.Start("http://sharepointfilerepair.codeplex.com/")
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Process.Start("http://sharepointworks.blogspot.com")
    End Sub
End Class
